'use strict';

var _ = require('lodash');
var Project = require('./project.model');
var Document = require('../document/document.model');
var Timeline = require('../timeline/timeline.model');

// Get list of projects
exports.index = function(req, res) {
  Project.find().populate(['leader','joiners', 'files', 'departments']).sort('created_at-').exec(function (err, projects) {
    if(err) { return handleError(res, err); }
    return res.json(200, projects);
  });
};

// Get list of projects
exports.me = function(req, res) {
  Project.find({leader: req.param('user')}).sort('created_at-').exec(function (err, projects) {
    if(err) { return handleError(res, err); }
    return res.json(200, projects);
  });
};

// Get a single project
exports.show = function(req, res) {
  Project.findById(req.params.id).populate(['leader','joiners', 'files']).exec(function (err, project) {
    if(err) { return handleError(res, err); }
    if(!project) { return res.send(404); }
    return res.json(project);
  });
};

// Get a single project
exports.showWithoutJoiners = function(req, res) {
  Project.findById(req.params.id).populate(['leader', 'files']).exec(function (err, project) {
    if(err) { return handleError(res, err); }
    if(!project) { return res.send(404); }
    return res.json(project);
  });
};

// Get a single project
exports.editshow = function(req, res) {
  Project.findById(req.params.id, function (err, project) {
    if(err) { return handleError(res, err); }
    if(!project) { return res.send(404); }
    return res.json(project);
  });
};

// Creates a new project in the DB.
exports.create = function(req, res) {
  Project.create(req.body, function(err, project) {
    if(err) { return handleError(res, err); }
    var newTimeline = {
      _user:project.leader,
      type: 'project',
      message: project.name
    };
    Timeline.create(newTimeline, function(err, timeline) {
      if(err) { return handleError(res, err); }
      console.log(timeline);
    });
    return res.json(201, project);
  });
};

// Updates an existing project in the DB.
exports.update = function(req, res) {
  //if(req.body._id) { delete req.body._id; }
  //Project.findById(req.params.id, function (err, project) {
  //  console.log(req.body);
  //  if (err) { return handleError(res, err); }
  //  if(!project) { return res.send(404); }
  //  var updated = _.merge(project, req.body);
  //  console.log(updated);
  //  //var updated = req.body;
  //  updated.save({$set:{departments: updated.departments, joiners: updated.joiners}}).exec(function (err) {
  //    if (err) { return handleError(res, err); }
  //    return res.json(200, project);
  //  });
  //});

  Project.findByIdAndUpdate(req.params.id, {$set: req.body}, function (err, project) {
    if (err) { return handleError(res, err); }
    if(!project) { return res.send(404); }
    return res.json(200, project);
  });
};

// Deletes a project from the DB.
exports.destroy = function(req, res) {
  Project.findById(req.params.id, function (err, project) {
    if(err) { return handleError(res, err); }
    if(!project) { return res.send(404); }
    project.remove(function(err) {
      if(err) { return handleError(res, err); }
      return res.send(204);
    });
  });
};

function handleError(res, err) {
  return res.send(500, err);
}
